﻿// ===================================
// The use and distribution terms for this software are covered by the Microsoft public license, 
// visit for more info : http://testcommander.codeplex.com 
// 
// You must not remove this copyright notice, or any other, from this software
// 
// Senthil Maruthaiappan  senips@gmail.com
// ===================================

using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using QAPro.Test.Addin.Fitnesse;
using QAPro.Test.FitServices.Interfaces;
using QAPro.Test.Interfaces;
using WiseLight.Framework.Lite.Container;

namespace QAPro.Test.Addin.FitnesseUI
{
    [InstanceBehaviour(typeof (ITestCase), InstanceBehaviourType.AlwaysCreate)]
    public class FitEditableLocalTestPage : FitLocalTestPage, IFitLocalTestNodeProperties, IFitRunnableDoc,
                                            IFitResultDoc, INotifyPropertyChanged
    {
        private string _modifedContent;

        public FitEditableLocalTestPage(string name)
            : base(name)
        {
        }

        [Bindable(true)]
        public string EditText
        {
            get { return ContentText; }
            set
            {
                _modifedContent = value;
                Save();
                Clear();
                Load();
                OnPropertyChanged("TestHtml");
            }
        }

        [Bindable(true)]
        public string TestHtml
        {
            get { return GetTestDocument(); }
            set { }
        }

        public string ResultHtml { get; set; }

        #region IFitLocalTestNodeProperties Members

        [Bindable(true)]
        public override string Name
        {
            get { return base.Name; }
            set
            {
                base.Name = value;
                OnPropertyChanged("Name");
            }
        }

        public override void Save()
        {
            if (!_modifedContent.Equals(EditText))
            {
                using (var writer = new StreamWriter(FullFileName))
                {
                    writer.Write(_modifedContent);
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public static FitEditableLocalTestPage RunTest(string tempWorkingFolderFolder, IServiceContext serviceContext,
                                                       string content)
        {
            string suiteFolder = tempWorkingFolderFolder + "\\Temp\\FitnesseRoot\\TestSuite\\";
            string testFolder = tempWorkingFolderFolder + "\\Temp\\FitnesseRoot\\TestSuite\\TestCase\\";
            if (!Directory.Exists(testFolder))
            {
                Directory.CreateDirectory(testFolder);
            }
            string contentFile = testFolder + "content.txt";
            using (var w = new StreamWriter(contentFile, false))
            {
                w.Write(content);
            }

            using (var w = new StreamWriter(testFolder + "properties.xml", false))
            {
                w.Write("<?xml version=\"1.0\"?> <properties>  <Test/>  </properties>");
            }

            var scriptProvider = serviceContext.Services.Get<IFitLocalTestScriptProvider>("testsession", suiteFolder);

            scriptProvider.LoadScripts();
            var fitUtilService = serviceContext.Services.Get<IFitUtilService>();
            IList<ITestNode> list = fitUtilService.GetRunnableTest(suiteFolder, scriptProvider);
            var testableItem = list[0] as FitEditableLocalTestPage;
            var fitTestEngine = serviceContext.Services.Get<IFitTestEngine>();
            fitTestEngine.Run(testableItem);
            var writer = serviceContext.Services.Get<ITestReportWriter>();

            (writer as TestReportDisplay).TestedItem = testableItem;
            writer.GenerateReport();
            testableItem.ResultHtml = (writer as TestReportDisplay).ResultHtml;


            return testableItem;
        }
    }
}